use std::{future::IntoFuture, ops::Index};

use bytes::Bytes;
// use crate::{minio_client::MinioClientState, models::*};
use anyhow::{anyhow, Error, Result};
use axum::{
    Router,
    routing::{get, post, delete},
    extract::{Extension, Path, Query, State},
    http::StatusCode,
    response::IntoResponse,
};
// use crate::http_helper::*;
use crate::{models, utils::{self, http_helper::*}};
use axum_macros::debug_handler;
use base64::prelude::*;
use chrono::Utc;
use serde::{Deserialize, Serialize};
use serde_json::json;
use tracing::{debug, error};
use uuid::Uuid;

// use sqlx::PgPool;
// use tracing::debug;
use crate::app_error::*;
use crate::models::auth::*;
use crate::models::metainfo::*;
use crate::AppState;
use reqwest::{self, Url};
use std::sync::Arc;
use tower_http::trace::TraceLayer;

pub fn routes(state: Arc<AppState>) -> Router {
    Router::new()
        .layer(TraceLayer::new_for_http())
        .route("/{user_id}", get(get_user_favorites))
        .with_state(state)
}

pub async fn get_user_favorites(
    State(app_state): State<Arc<AppState>>,
    _: UserClaims,
    Path(user_id): Path<i64>,
) -> Result<impl IntoResponse, AppError>  {
    let songs = app_state.pg_dao.user_favorites_dao.get_favorite_songs(user_id).await?;

    Ok(AppJson(SuccessResponse {
        code: 200,
        message: "ok".to_string(),
        data: json!({
            "songs": songs
        }),
    }))
}

pub async fn add_user_favorite(
    State(app_state): State<Arc<AppState>>,
    _: UserClaims,
    Path(user_id): Path<i64>,
    Path(song_id): Path<i64>,
) -> Result<impl IntoResponse, AppError>  {
    app_state.pg_dao.user_favorites_dao.add_favorite_song(user_id, song_id).await?;

    Ok(AppJson(SuccessResponse {
        code: 200,
        message: "ok".to_string(),
        data: json!({}),
    }))
}

pub async fn delete_user_favorite(
    State(app_state): State<Arc<AppState>>,
    _: UserClaims,
    Path(user_id): Path<i64>,
    Path(song_id): Path<i64>,
) -> Result<impl IntoResponse, AppError>  {
    app_state.pg_dao.user_favorites_dao.remove_favorite_song(user_id, song_id).await?;
    Ok(AppJson(SuccessResponse {
        
        code: 200,
        message: "ok".to_string(),
        data: ""
    }))
}
